<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace app\pack\admin;

use app\common\builder\ZBuilder;
use app\admin\controller\Admin;
use app\pack\model\Pack as PackModel;
use app\pack\model\PackUser as PackUserModel;
use app\admin\model\Menu as MenuModel;
use util\Tree;
use think\Db;

/**
 * 套餐管理
 * @package app\admin\controller
 */
class Index extends Admin{
    public function index()
    {
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder('create_time desc');
        $map['is_delete'] = 0;


        // 数据列表
        $data_list = PackModel::where($map)->order($order)->paginate();
        // 加入角色
        $role = session('user_auth.role');
        $uid = session('user_auth.uid');
        if($role!=1 && $role!=2){
             $packUser = PackUserModel::where(array('uid' =>$uid))->find();
            $pack = $packUser['pack'];
            foreach ($data_list as $k => $v) {
                if(!in_array($v['id'],$pack)){
                    unset($data_list[$k]);
                }
            }
        }
        // zje 写到这里。
        return ZBuilder::make('table')
            ->setSearch(['title' => '套餐名称']) // 设置搜索框
            ->addColumns([ // 批量添加数据列
                ['id', 'ID'],
                ['title', '套餐名称', 'text'],
                ['create_time', '创建时间', 'datetime'],
                ['status', '状态','status', '', ['未发布', '发布']],
                ['is_delete','删除状态','status', '', ['未删除:info', '删除:danger']],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons(['edit', 'delete' => ['data-tips' => '删除后无法恢复。']]) // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染模板
    }
    /**
     * 添加套餐
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!isset($data['plugin'])) {
                $data['plugin'] = [];
            }
            $data['create_time']=time();
            $data['is_delete'] =0;
            if ($user = PackModel::create($data)) {
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }
        // 菜单列表
        $menus = cache('access_menus');
        if (!$menus) {
            $modules = Db::name('admin_module')->where('status', 1)->column('name');
            $menus_where['id'] = ['not in',['4','68','216']];
            $menus_where['module'] = ['in',$modules];
            $menus = MenuModel::where($menus_where)->order('sort,id')->column('id,pid,sort,title,icon');
            $menus = Tree::toLayer($menus);
            // 非开发模式，缓存菜单
            if (config('develop_mode') == 0) {
                cache('access_menus', $menus);
            }
        }
        $this->assign('menus', $menus);
        return $this->fetch();
    }
    /**
     * 编辑套餐
     * @param null $id
     * @return mixed
     */
    public function edit($id = null)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!isset($data['plugin'])) {
                $data['plugin'] = [];
            }
            $where['id'] = $data['id'];
            if (PackModel::update($data, $where)) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
        $where['id'] = $id;
        $info = PackModel::get($id);
        $modules    = Db::name('admin_module')->where('status', 1)->column('name');
        $menus_where['id'] = ['not in',['4','68','216']];
        $menus_where['module'] = ['in',$modules];
        $menus      = MenuModel::where($menus_where)->order('sort,id')->column('id,pid,sort,title,icon');
        $this->assign('menus', Tree::toLayer($menus));
        $this->assign('info', $info);
        return $this->fetch('edit');
        
    }
    public function delete($record = [])
    {
        $param = $this->request->param();
        $ids = $param['ids'];
        $where['id'] = $ids;
        if (is_array($ids)) {
            $where['id'] = array('in', $ids);
        }
        $data['is_delete'] = 1;
        if (PackModel::update($data, $where)) {
            $this->success('修改成功', url('index'));
        } else {
            $this->error('修改失败');
        }



    }
}
